﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConverterInterface;

namespace DelphiConverterLibrary
{
    public class StringState : State
    {
        private StringState()  { }

        private static StringState instance = new StringState();

        public static StringState Instance
        {
            get {return instance;}
        }

        private char c;
        public override void Execute(DelphiConverter owner)
        {
            if (owner.AvailableChars() <= 0)
            {
                owner.machine.Finished = true;
                owner.machine.ChangeState(NothingState.Instance);
            }
            else
            {
                c = owner.Peek();
                if (c == '\r')
                {
                    owner.machine.ChangeState(NothingState.Instance);
                }
                else
                {
                    owner.Forward();
                    owner.machine.Buffer.Append(c);
                    if (c == '\'')
                    { 
                        owner.machine.ChangeState(NothingState.Instance);
                    }
                }

            }
        }

        public override void Exit(DelphiConverter owner)
        {
            owner.machine.Buffer.Replace("\"", "&quot;");
            owner.machine.Buffer.Replace("<", "&lt;");
            owner.machine.Buffer.Replace(">", "&gt;");
            owner.machine.Buffer.Insert(0, "<span class=\'string\'>");
            owner.machine.Buffer.Append("</span>");
            owner.FlushBuffer();
        }

    }
}

